package com.chixing.ih.module.admin.service.Impl;

import com.chixing.ih.module.admin.mapper.AdminTaskMapper;
import com.chixing.ih.module.admin.service.IAdminTaskService;
import com.chixing.ih.module.system.pojo.ToEmail;
import com.chixing.ih.module.task.pojo.Task;
import com.chixing.ih.util.MailUtil;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AdminTaskServiceImpl implements IAdminTaskService {

    @Autowired
    private AdminTaskMapper adminTaskMapper;

    @Autowired
    MailUtil mailUtil;

    @Override
    public List<Task> getPendingTasks(Integer pageNum, Integer pageSize,String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        return adminTaskMapper.getPendingTasks(keyword);
    }

    @Override
    public List<Task> getAuditedTasks(Integer pageNum, Integer pageSize,String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        return adminTaskMapper.getAuditedTasks(keyword);
    }

    @Override
    public List<Task> getRefusedTasks(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        return adminTaskMapper.getRefusedTasks(keyword);
    }

    @Override
    public boolean applicationApproved(Integer id) {
        return adminTaskMapper.applicationApproved(id);
    }

    @Override
    public boolean rejectApplication(Integer id) {
        return adminTaskMapper.rejectApplication(id);
    }

    @Override
    public boolean resetApplication(Integer id) {
        return adminTaskMapper.resetApplication(id);
    }



    public void sendApplicationApprovedEmail(String email) {
        String[] arr = email.split("\"");
        email = arr[1];
        ToEmail toEmail = new ToEmail();
        toEmail.setSubject("IT帮任务审核通知");
        toEmail.setContent("恭喜您发布的任务通过审核。");
        toEmail.setFrom("wy2668461654@163.com");
        toEmail.setTo(email);
        mailUtil.sendOneEmail(toEmail);
        System.out.println("邮件发送成功!");
    }

    public void sendRejectApplicationEmail(String email) {
        String[] arr = email.split("\"");
        email = arr[1];
        ToEmail toEmail = new ToEmail();
        toEmail.setSubject("IT帮任务审核通知");
        toEmail.setContent("很遗憾的通知您，你的任务没有通过审核。");
        toEmail.setFrom("wy2668461654@163.com");
        toEmail.setTo(email);
        mailUtil.sendOneEmail(toEmail);
        System.out.println("邮件发送成功!");
    }

}
